<template>
<view class="body">
	<view class="header">
		<scroll-view scroll-y="true" class="history" :scroll-into-view="toView">
			<block v-for="(item,index) in list" :key="index" class="content">
				<view class='my_right' v-if="item.type == 1" :id='"AM"+index'>
					<text class="time" v-if="item.addtime">{{item.addtime}}</text>
					<view class='p_r'>
						<view class='new_txt'>
							<view class='new_txt_my'>{{item.content}}</view>
						</view>
						<image class='new_img' :src='uimg'></image>
					</view>
				</view>
				<view class='you_left' v-else :key="index" :id='"AM"+index'>
					<text class="time" v-if="item.addtime">{{item.addtime}}</text>
					<view class='p_r1'>
						<view class="p_top">
							<image class='new_img1' src='/static/img/logo.png'></image>
							<view class='new_txt' v-if="item.role">
								<view class='new_txt_my1'>
								  <text v-for="(iitem,iindex) in item.content" :key="iindex" @click="answer(iitem.value)">{{iitem.name}}</text>
								</view>
							</view>
							<view class='new_txt' v-else>
								<view class='new_txt_my'>{{item.content}}</view>
							</view>
						</view>
						<view class="contact" @click="jumpKf">转小程序客服,电话:18322223377</view>
					</view>
				</view>
			</block>
		</scroll-view>
	</view>
	<view class="sendmessage">
		<view @click="remove" class='remove'>清空</view>
		<input type="text" @input="bindKeyInput" :value='inputValue'
		confirm-type = 'send' cursor-spacing = '10' @confirm="search"/>
		<view @click="search" class='user_input_text'>发送</view>
		<!-- <view class="send">
			<view @click="remove" class='remove'>清空</view>
			<input type="text" @input="bindKeyInput" :value='inputValue'  
			confirm-type = 'send' cursor-spacing = '10' @confirm="search"/>
			<view @click="search" class='user_input_text'>发送</view>
		</view> -->
	</view>
</view>
</template>
<script>
import request from '@/util/request.js'
export default {
	data() {
		return {
			scrollHeight: '100vh',
			uid:uni.getStorageSync('uid'),
			list:[],
			inputValue:'',
			toView:'',
			avatar:''
		}
	},
	onLoad() {
		if(uni.getStorageSync('list')){
		    var str = uni.getStorageSync('list').length - 1;
		    this.list = uni.getStorageSync('list')
			this.$nextTick(()=> {
				this.toView = 'AM' + str
			});
		}
		this.auto()
		this.getInfo()
	},
	methods: {
		getInfo:function(){
		    var that = this
		    var data = {
				'uid':uni.getStorageSync('uid')
			}
			request({
				url: 'member/status',
				method: 'get',
				data:data,
				success: (res) => {
					if(res.data.code == 1){
						that.avatar = res.data.data.avatar
					}
				}
			})
		},
		auto:function(){
			var that = this
			request({
				url: 'problem/list',
				method: 'get',
				success: (res) => {
					if(res.data.code == 1){
						var arr = res.data.data
						var arr1 = {'type':2, 'content':arr,'role':1}
						var list = this.list
						list.push(arr1)
						var str = list.length - 1;
						that.$nextTick(()=> {
							that.toView = 'AM' + str
						});
						that.list = list
					}
				}
			})
		},
		send:function(){
			if(!this.inputValue){
			    return;
			}
			var that = this
			var arr = {'type':1,'content':this.inputValue}
			var data = {'name':this.inputValue}
			var list = this.list
			request({
				url: 'problem/info',
				method: 'post',
				data:data,
				success: (res) => {
					list.push(arr)
					if(res.data.code == 1 && res.data.data){
						var arr1 = {'type':2,'content':res.data.data.value}
					}else{
						var arr1 = {'type':2,'content':'机器人小美为您服务,如有更多问题请转人工客服,客服电话:18322223377'}
					}
					list.push(arr1)
					var str = list.length - 1;
					that.inputValue = ''
					that.list = list
					that.$nextTick(()=> {
						that.toView = 'AM' + str
					});
					uni.setStorageSync('list',list)
				}
			})
		},
		bindKeyInput:function(e){
			this.inputValue = e.detail.value
		},
		search:function(){
			this.send()
		},
		remove:function(){
		    uni.removeStorageSync('list')
		    this.list = []
		},
		answer:function(item){
			var arr1 = {'type':2,'content':item}
			var list = this.list
			list.push(arr1)
			var str = list.length - 1;
			this.$nextTick(()=> {
				this.toView = 'AM' + str
			});
			this.list = list
		},
		jumpKf:function(){
			plus.share.getServices(
				res => {
					let sweixin = null;
					for (let i in res) {
						if (res[i].id == 'weixin') {
							sweixin = res[i];
						}
					}
					//唤醒微信小程序
					if (sweixin) {
						sweixin.launchMiniProgram({
							id: 'gh_42fb5eed1670', //
							type: 0, //小程序版本  0-正式版； 1-测试版； 2-体验版。
							path: 'pages/index/index' //小程序的页面,用传的参数在小程序接值判断跳转指定页面
						});
					}
				}
			);
		}
	},
}
</script>
<style scoped>
	@import url("./css/index.css");
</style>